{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 包管理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 3)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2],\n",
       "       [ 3,  4,  5],\n",
       "       [ 6,  7,  8],\n",
       "       [ 9, 10, 11]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "3.141592653589793"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "1.3694384060045657"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "0.9950041652780257"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "1.7320508075688772"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 包管理\n",
    "from numpy import *\n",
    "import math\n",
    "arr = arange(12).reshape(4,3)\n",
    "arr.shape\n",
    "arr\n",
    "pai = math.pi\n",
    "pai\n",
    "math.floor(pai)\n",
    "int(pai)\n",
    "math.acos(0.2)\n",
    "math.cos(0.1)\n",
    "math.sqrt(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 变量管理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "('string', str)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(1, int)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(2.0, float)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(30000000000000, int)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "((1, 2, 3), tuple)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "({'age': 38, 'name': 'haijian'}, dict)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "([2.0, 3.0, 4.0], list)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'我'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'是'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'曾'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'海'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'剑'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'<html>\\n\"\"i\\'am haijian\"\\nasdfasdfasd\\n</html>'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'string'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[1, 1.0]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'haijian'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "37"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多赋值\n",
    "\n",
    "p = (4, 5)\n",
    "x, y = p\n",
    "x\n",
    "y\n",
    "\n",
    "data = ['string', 1, 2.0, 30000000000000, (1, 2, 3), {'name': 'haijian', 'age': 38}, [2.0, 3.0, 4.], 'string2']\n",
    "elm1, elm2, elm3, elm4, elm5, elm6, elm7, _ = data\n",
    "elm1, type(elm1)\n",
    "elm2, type(elm2)\n",
    "elm3, type(elm3)\n",
    "elm4, type(elm4)\n",
    "elm5, type(elm5)\n",
    "elm6, type(elm6)\n",
    "elm7, type(elm7)\n",
    "\n",
    "my_str1 = \"我是曾海剑\"\n",
    "\n",
    "c1, c2, c3, c4, c5 = my_str1\n",
    "c1\n",
    "c2\n",
    "c3\n",
    "c4\n",
    "c5\n",
    "\n",
    "# 变量\n",
    "my_str2 = \"\"\"<html>\n",
    "\"\"i'am haijian\"\n",
    "asdfasdfasd\n",
    "</html>\"\"\"\n",
    "\n",
    "my_str2\n",
    "\n",
    "arr1 = [\"string\", 1, 1.0, 2000]\n",
    "arr1[0]\n",
    "arr1[1]\n",
    "arr1[1:-1]\n",
    "\n",
    "d = {'name': 'haijian', 'age': 37}\n",
    "d['name']\n",
    "d['age']\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Dave'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'dave@example.com'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['773-555-1212', '847-555-1212']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "([10, 8, 7, 1, 9, 5, 10], 3)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[2, 3, 4]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "foo 1 2\n",
      "bar hello\n",
      "foo 3 4\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "('nobody', '/var/empty', '/usr/bin/false')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "55"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不定长参数\n",
    "\n",
    "record = ('Dave', 'dave@example.com', '773-555-1212', '847-555-1212')\n",
    "name, email, *phone_numbers = record\n",
    "name\n",
    "email\n",
    "phone_numbers\n",
    "\n",
    "*trailing, current = [10, 8, 7, 1, 9, 5, 10, 3]\n",
    "trailing, current\n",
    "\n",
    "def drop_first_last(*grades):\n",
    "    first, *middle, last = grades\n",
    "    return (middle)\n",
    "\n",
    "drop_first_last(1, 2, 3, 4, 5)\n",
    "\n",
    "records = [\n",
    "    ('foo', 1, 2),\n",
    "    ('bar', 'hello'),\n",
    "    ('foo', 3, 4),\n",
    "]\n",
    "\n",
    "def do_foo(x, y):\n",
    "    print('foo', x, y)\n",
    "\n",
    "def do_bar(s):\n",
    "    print('bar', s)\n",
    "\n",
    "for tag, *args in records:\n",
    "    if tag == 'foo':\n",
    "        do_foo(*args)\n",
    "    elif tag == 'bar':\n",
    "        do_bar(*args)\n",
    "        \n",
    "line = 'nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false'\n",
    "uname, *_, homedir, sh = line.split(':')\n",
    "uname, homedir, sh\n",
    "\n",
    "def sum(items):\n",
    "    head, *tail = items\n",
    "    return head + sum(tail) if tail else head\n",
    "\n",
    "sum([1,2,3,4,5,6,7,8,9,10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "deque([2, 3, 4])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "deque([4, 1, 2, 5, 3])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 保留最后 N 个元素\n",
    "\n",
    "# deque是固定长度队列，当append新元素的时候，如果已经到达maxlen，那么会把最早的元素挤出队列\n",
    "import collections\n",
    "\n",
    "q1 = collections.deque(maxlen=3)\n",
    "q1.append(1)\n",
    "q1.append(2)\n",
    "q1.append(3)\n",
    "q1.append(4)\n",
    "\n",
    "q1\n",
    "\n",
    "q2 = collections.deque()\n",
    "q2.append(1)\n",
    "q2.append(2)\n",
    "q2.append(3)\n",
    "q2.appendleft(4)\n",
    "\n",
    "q2.insert(-1, 5)\n",
    "q2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.57510517, -1.07000422, -0.46497176, -0.28413993, -0.0105018 ,\n",
       "        0.5970966 ,  0.97685338,  1.10478463,  1.1253005 ,  2.01111201])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "array([-1.57510517, -1.07000422, -0.46497176])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[2.0111120076370734,\n",
       " 1.1253005013327722,\n",
       " 1.104784630413755,\n",
       " 0.97685337568408837,\n",
       " 0.59709659855067954]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[-1.5751051653340236,\n",
       " -1.0700042216988876,\n",
       " -0.46497176343281632,\n",
       " -0.28413992934110582,\n",
       " -0.010501801176449848]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "('cheap',\n",
       " [{'name': 'YHOO', 'price': 16.35, 'shares': 45},\n",
       "  {'name': 'FB', 'price': 21.09, 'shares': 200},\n",
       "  {'name': 'HPQ', 'price': 31.75, 'shares': 35}])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "('expensive',\n",
       " [{'name': 'AAPL', 'price': 543.22, 'shares': 50},\n",
       "  {'name': 'ACME', 'price': 115.65, 'shares': 75},\n",
       "  {'name': 'IBM', 'price': 91.1, 'shares': 100}])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "('portfolio:',\n",
       " [{'name': 'FB', 'price': 21.09, 'shares': 200},\n",
       "  {'name': 'IBM', 'price': 91.1, 'shares': 100},\n",
       "  {'name': 'ACME', 'price': 115.65, 'shares': 75},\n",
       "  {'name': 'AAPL', 'price': 543.22, 'shares': 50},\n",
       "  {'name': 'YHOO', 'price': 16.35, 'shares': 45},\n",
       "  {'name': 'HPQ', 'price': 31.75, 'shares': 35}])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import heapq\n",
    "\n",
    "arr1 = np.random.normal(size=[10])\n",
    "arr1.sort(axis=0)\n",
    "arr1\n",
    "arr1[:3]\n",
    "\n",
    "heapq.nlargest(5, arr1)\n",
    "heapq.nsmallest(5, arr1)\n",
    "\n",
    "portfolio = [\n",
    "    {'name': 'IBM', 'shares': 100, 'price': 91.1},\n",
    "    {'name': 'AAPL', 'shares': 50, 'price': 543.22},\n",
    "    {'name': 'FB', 'shares': 200, 'price': 21.09},\n",
    "    {'name': 'HPQ', 'shares': 35, 'price': 31.75},\n",
    "    {'name': 'YHOO', 'shares': 45, 'price': 16.35},\n",
    "    {'name': 'ACME', 'shares': 75, 'price': 115.65}\n",
    "]\n",
    "cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])\n",
    "expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])\n",
    "\n",
    "\"cheap\", cheap\n",
    "\"expensive\", expensive\n",
    "\n",
    "portfolio.sort(key=lambda i: i['shares'], reverse=True)\n",
    "\"portfolio:\", portfolio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defaultdict(list, {'a': [1, 2], 'b': [3.2], 'c': '3', 'd': 3, 'e': {'k': 'v'}})"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'd', 'e', 'c']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'{\"a\": [1, 2], \"b\": [3.2], \"d\": 3, \"e\": {\"k\": \"v\"}, \"c\": \"3\"}'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'{\"k1\": [{\"kk\": \"vv\"}, {\"kk\": \"vv2\"}]}'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(10.75, 'FB')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(612.78, 'AAPL')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[(612.78, 'AAPL'),\n",
       " (205.55, 'IBM'),\n",
       " (45.23, 'ACME'),\n",
       " (37.2, 'HPQ'),\n",
       " (10.75, 'FB')]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'a', 'b'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'c'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "dict_keys(['ACME', 'AAPL', 'IBM', 'HPQ', 'FB'])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "dict_values([45.23, 612.78, 205.55, 37.2, 10.75])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "dict_keys"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import collections\n",
    "d = collections.defaultdict(list)\n",
    "pairs = [('a', 1), (\"a\", 2), (\"b\", 3.2)]\n",
    "for key, value in pairs:\n",
    "    d[key].append(value)\n",
    "\n",
    "d['d'] = 3\n",
    "d['e'] = {\"k\": \"v\"}\n",
    "d['c'] = \"3\"  \n",
    "d\n",
    "\n",
    "list(d)\n",
    "\n",
    "import json\n",
    "json.dumps(d)\n",
    "\n",
    "json.dumps({\"k1\": [ {\"kk\": \"vv\"}, {\"kk\": \"vv2\"}]})\n",
    "\n",
    "prices = {\n",
    "    'ACME': 45.23,\n",
    "    'AAPL': 612.78,\n",
    "    'IBM': 205.55,\n",
    "    'HPQ': 37.20,\n",
    "    'FB': 10.75\n",
    "}\n",
    "min_price = min(zip(prices.values(), prices.keys()))\n",
    "max_price = max(zip(prices.values(), prices.keys()))\n",
    "min_price\n",
    "max_price\n",
    "\n",
    "sorted(zip(prices.values(), prices.keys()),reverse=True)\n",
    "\n",
    "d1 = {\"a\", \"b\", \"c\"}\n",
    "d2 = {\"c\", \"d\"}\n",
    "d1 - d2\n",
    "d1 & d2\n",
    "prices.keys() \n",
    "prices.values()\n",
    "type(prices.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[1, 3, 2, 5, 3, 4]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "set"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "set"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "####\n",
      "1\n",
      "3\n",
      "5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(1, 1, 2)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 消除重复元素\n",
    "\n",
    "arr1 = [1, 3, 2, 5, 3, 4]\n",
    "type(arr1)\n",
    "set(arr1)\n",
    "\n",
    "list(arr1)\n",
    "\n",
    "arr2 = {1, 2, 3, 3, 4}\n",
    "arr2\n",
    "type(arr2)\n",
    "\n",
    "type([1,2,3])\n",
    "type({1,2,3})\n",
    "type({\"name\": \"haijian\", \"age\":38})\n",
    "type((1, 2))\n",
    "type(1)\n",
    "\n",
    "set1 = {1,3,2}\n",
    "for s in set1:\n",
    "    print(s)\n",
    "    \n",
    "def add(x,y):\n",
    "    return x+y\n",
    "\n",
    "# 切片\n",
    "print(\"####\")\n",
    "s = slice(1,6,2)    \n",
    "arr = range(10)\n",
    "for i in arr[s]:\n",
    "    print(i)\n",
    "s.indices(1)    \n",
    "s.start\n",
    "s.stop\n",
    "s.step\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('eyes', 8), ('the', 5), ('look', 4)]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "dict_values([4, 3, 3, 8, 5, 1, 2, 1, 1, 1])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "dict_keys(['look', 'into', 'my', 'eyes', 'the', 'not', 'around', \"don't\", \"you're\", 'under'])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "words = [\n",
    "    'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',\n",
    "    'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',\n",
    "    'eyes', \"don't\", 'look', 'around', 'the', 'eyes', 'look', 'into',\n",
    "    'my', 'eyes', \"you're\", 'under'\n",
    "]\n",
    "from collections import Counter\n",
    "word_counts = Counter(words)\n",
    "# 出现频率最高的3个单词\n",
    "top_three = word_counts.most_common(3)\n",
    "print(top_three)\n",
    "word_counts.values()\n",
    "word_counts.keys()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'fname': 'Big', 'lname': 'Jones', 'uid': 1004},\n",
       " {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},\n",
       " {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},\n",
       " {'fname': 'John', 'lname': 'Cleese', 'uid': 1001}]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[{'fname': 'John', 'lname': 'Cleese', 'uid': 1001},\n",
       " {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},\n",
       " {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},\n",
       " {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},\n",
       " {'fname': 'John', 'lname': 'Cleese', 'uid': 1001},\n",
       " {'fname': 'Big', 'lname': 'Jones', 'uid': 1004},\n",
       " {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003}]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[{'fname': 'John', 'lname': 'Cleese', 'uid': 1001},\n",
       " {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},\n",
       " {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},\n",
       " {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "07/01/2012\n",
      "  {'address': '5412 N CLARK', 'date': '07/01/2012'}\n",
      "  {'address': '4801 N BROADWAY', 'date': '07/01/2012'}\n",
      "07/02/2012\n",
      "  {'address': '5800 E 58TH', 'date': '07/02/2012'}\n",
      "  {'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'}\n",
      "  {'address': '1060 W ADDISON', 'date': '07/02/2012'}\n",
      "07/03/2012\n",
      "  {'address': '2122 N CLARK', 'date': '07/03/2012'}\n",
      "07/04/2012\n",
      "  {'address': '5148 N CLARK', 'date': '07/04/2012'}\n",
      "  {'address': '1039 W GRANVILLE', 'date': '07/04/2012'}\n"
     ]
    }
   ],
   "source": [
    "rows = [\n",
    "    {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},\n",
    "    {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},\n",
    "    {'fname': 'John', 'lname': 'Cleese', 'uid': 1001},\n",
    "    {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}\n",
    "]\n",
    "# 根据任意的字典字段来排序输入结果行是很容易实现的，代码示例：\n",
    "\n",
    "from operator import itemgetter\n",
    "rows_by_fname = sorted(rows, key=itemgetter('fname'))\n",
    "rows_by_uid = sorted(rows, key=itemgetter('uid'))\n",
    "rows_by_fname\n",
    "rows_by_uid\n",
    "sorted(rows, key=lambda r: (r['lname'], r['fname']))\n",
    "sorted(rows, key=lambda r: r['uid'])\n",
    "\n",
    "\n",
    "\n",
    "rows = [\n",
    "    {'address': '5412 N CLARK', 'date': '07/01/2012'},\n",
    "    {'address': '5148 N CLARK', 'date': '07/04/2012'},\n",
    "    {'address': '5800 E 58TH', 'date': '07/02/2012'},\n",
    "    {'address': '2122 N CLARK', 'date': '07/03/2012'},\n",
    "    {'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},\n",
    "    {'address': '1060 W ADDISON', 'date': '07/02/2012'},\n",
    "    {'address': '4801 N BROADWAY', 'date': '07/01/2012'},\n",
    "    {'address': '1039 W GRANVILLE', 'date': '07/04/2012'},\n",
    "]\n",
    "from operator import itemgetter\n",
    "from itertools import groupby\n",
    "\n",
    "# Sort by the desired field first\n",
    "rows.sort(key=itemgetter('date'))\n",
    "# Iterate in groups\n",
    "for date, items in groupby(rows, key=itemgetter('date')):\n",
    "    print(date)\n",
    "    for i in items:\n",
    "        print(' ', i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 8, 14, 2, 6, 10]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[10, 8, 14, 2, 6, 10]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[10, 8, 14, 2, 0, 6, 0, 0, 10]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[True, True, True, False, False, True, False, False, True]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "55"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'AAPL': 612.78, 'IBM': 205.55}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'AAPL': 612.78, 'HPQ': 37.2, 'IBM': 205.55}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[50, 75, 20, 65]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n",
      "75\n",
      "20\n",
      "65\n"
     ]
    }
   ],
   "source": [
    "# 列表推导\n",
    "\n",
    "# 获取大于0的元素，并且把元素*2\n",
    "mylist = [5, 4, 7, 1, -1, 3, 0, -2, 5]\n",
    "# [func(item) for item in items filter_condition(item)]\n",
    "[n*2 for n in mylist if n > 0]\n",
    "\n",
    "# 相当于上边的列表推导\n",
    "mylist2 = map(lambda i: i*2, mylist)\n",
    "list(filter(lambda i: i>0, mylist2))\n",
    "\n",
    "# 把小于0的元素变成0，并且把元素*2\n",
    "[n*2 if n >0 else 0 for n in mylist]\n",
    "[n > 1 for n in mylist]\n",
    "\n",
    "nums = [1, 2, 3, 4, 5]\n",
    "sum(x * x for x in nums)\n",
    "sum(nums)\n",
    "min(nums)\n",
    "\n",
    "# 字典推导\n",
    "prices = {\n",
    "    'ACME': 45.23,\n",
    "    'AAPL': 612.78,\n",
    "    'IBM': 205.55,\n",
    "    'HPQ': 37.20,\n",
    "    'FB': 10.75\n",
    "}\n",
    "# Make a dictionary of all prices over 200\n",
    "{key: value for key, value in prices.items() if value > 200}\n",
    "# Make a dictionary of tech stocks\n",
    "tech_names = {'AAPL', 'IBM', 'HPQ', 'MSFT'}\n",
    "{key: value for key, value in prices.items() if key in tech_names}\n",
    "\n",
    "portfolio = [\n",
    "    {'name':'GOOG', 'shares': 50},\n",
    "    {'name':'YHOO', 'shares': 75},\n",
    "    {'name':'AOL', 'shares': 20},\n",
    "    {'name':'SCOX', 'shares': 65}\n",
    "]\n",
    "[s['shares'] for s in portfolio]\n",
    "min(s['shares'] for s in portfolio)\n",
    "\n",
    "# for生成器，相当于创建了一个iterator，iterator只能运行一次\n",
    "gen = (s['shares'] for s in portfolio)\n",
    "for s in gen:\n",
    "    print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': -1, 'b': -2, 'x': 1, 'y': 2, 'z': 4}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[('x', 1), ('y', 2), ('y', 3), ('z', 4), ('a', -1), ('b', -2)]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'a': -1, 'b': -2, 'x': 1, 'y': 3, 'z': 4}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = {\"x\": 1, \"y\":2}\n",
    "b = {\"y\": 3, \"z\":4}\n",
    "c = {\"a\": -1, \"b\": -2}\n",
    "import collections\n",
    "dict(collections.ChainMap(a,b,c))\n",
    "\n",
    "l1 = list(zip(a.keys(), a.values()))\n",
    "l2 = list(zip(b.keys(), b.values()))\n",
    "l3 = list(zip(c.keys(), c.values()))\n",
    "l1 + l2 + l3\n",
    "dict(l1 + l2 + l3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
